package com.jy.adscm.operation.pebase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.pebase.bean.PeAbstractContent;
import com.jy.adscm.operation.pebase.bean.PeApplyHistory;
import com.jy.adscm.operation.pebase.mapper.mysql.PeAbstractContentMapper;
import com.jy.adscm.operation.pebase.mapper.mysql.PeApplyHistoryMapper;
import com.jy.adscm.operation.pebase.service.PeApplyHistoryService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-21
 */
@Service
public class PeApplyHistoryServiceImpl extends ServiceImpl<PeApplyHistoryMapper, PeApplyHistory> implements PeApplyHistoryService {
     @Autowired
    JyUserMapper jyUserMapper;
     @Autowired
    PeAbstractContentMapper peAbstractContentMapper;
    @Override
    public boolean updatePeApplyHistory(PeApplyHistory peApplyHistory, Token token) {
        PeApplyHistory oldpeApplyHistory = super.selectById(peApplyHistory.getId());
        if (AdscmBaseUtils.getInstance().isNotEmpty(peApplyHistory.getApproveResult(),null)){
            if (!AdscmBaseUtils.getInstance().isNotEmpty(oldpeApplyHistory.getApproveResult(),null)){
                //审核通过
                if (peApplyHistory.getApproveResult()==1){
                    PeAbstractContent peAbstractContent = peAbstractContentMapper.selectById(peApplyHistory.getPid());
                    peAbstractContent.setContent(peApplyHistory.getAfterContent());
                    peAbstractContentMapper.updateById(peAbstractContent);
                }
                peApplyHistory.setApprover(token.getUser().getId().toString());
                peApplyHistory.setApproveTime(new Date());
            }
        }
        boolean b = updateById(peApplyHistory);
        return b;
    }

    @Override
    public List<PeApplyHistory> selectByDid(Integer id) {
        Wrapper<PeApplyHistory> wrapper=new EntityWrapper<>();
        wrapper.eq("pid",id);
        List<PeApplyHistory> peApplyHistories = super.selectList(wrapper);
        for (PeApplyHistory peApplyHistory : peApplyHistories) {
         if (AdscmBaseUtils.getInstance().isNotEmpty(peApplyHistory.getApprover(),null)){
             JyUser jyUser = jyUserMapper.selectById(peApplyHistory.getApprover());
             peApplyHistory.setApproverName(jyUser.getName());
         }
         if (AdscmBaseUtils.getInstance().isNotEmpty(peApplyHistory.getCreator(),null)){
             JyUser jyUser = jyUserMapper.selectById(peApplyHistory.getCreator());
             peApplyHistory.setCreatorName(jyUser.getName());
         }
        }
        return peApplyHistories;
    }
}
